package com.system.weirdor.search.service.impl;

import com.system.weirdor.search.index.EsCorporate;
import com.system.weirdor.search.repository.EsCorporateRepository;
import com.system.weirdor.search.service.EsCorporateService;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EsCorporateServiceImpl implements EsCorporateService {
    
    @Autowired
    private EsCorporateRepository esCorporateRepository;

    public void index(List<EsCorporate> list) {
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        esCorporateRepository.save(list);
    }

    public List<EsCorporate> search(String keyword){
        QueryBuilder queryBuilder = new FuzzyQueryBuilder("name", keyword);
        Pageable pageable = new PageRequest(1, 10);
       List<EsCorporate> page = esCorporateRepository.findByName(keyword);
        return  page;
    }

	public EsCorporate save(EsCorporate escorporate) {
		// TODO Auto-generated method stub
		return esCorporateRepository.save(escorporate);
	}


}
